package vn.edu.uit.chemistry;

import vn.edu.uit.chemistry.dragdrop.DragController;
import vn.edu.uit.chemistry.dragdrop.DragLayer;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class GuiBaiKiemTra extends Activity {

	private DragController mDragController;
	private DragLayer mDragLayer;

	ImageView imgNopBai;
	ImageView imgMailBox;

	TextView tvTitle;

	String dapanHS = "";
	String kiemtra = "";

	public static String content;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mDragController = new DragController(this);

		setContentView(R.layout.screen_nop_bai);

		Intent startIntent = getIntent();
		if (startIntent != null) {
			Bundle b = startIntent.getExtras();
			dapanHS = b.getString("dapanHS");
			kiemtra = b.getString("kiemtra");
		}

		tvTitle = (TextView) findViewById(R.id.tvTitle);
		mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
		mDragLayer.setDragController(mDragController);
		mDragController.addDropTarget(mDragLayer);

		imgNopBai = (ImageView) findViewById(R.id.imgNopBai);
		imgNopBai.setOnLongClickListener(new OnLongClickListener() {

			public boolean onLongClick(View v) {
				v.setContentDescription(kiemtra + "#" + dapanHS);
				boolean handledHere = false;
				handledHere = startDrag(v);
				return true;
			}
		});

		mDragController.delActivity(this, this, true);

		imgMailBox = (ImageView) findViewById(R.id.mailboxZone);
		imgMailBox.postDelayed(onEverySecond, 0);

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		if (kiemtra.equals("BKT15")) {
			tvTitle.setText("Nộp Bài Kiểm Tra 15 Phút");
		} else if (kiemtra.equals("BKT45")) {
			tvTitle.setText("Nộp Bài Kiểm Tra 45 Phút");
		}
	}

	public boolean startDrag(View v) {
		Object dragInfo = v;
		mDragController.startDrag(v, mDragLayer, dragInfo,
				DragController.DRAG_ACTION_MOVE);
		return true;
	}

	public Runnable onEverySecond = new Runnable() {
		public void run() {
			new GetCoodinationAsyncTask().execute();
		}
	};

	final class GetCoodinationAsyncTask extends AsyncTask<Void, Void, Void> {

		int xCo = 0;
		int yCo = 0;

		public GetCoodinationAsyncTask() {
			//
		}

		@Override
		protected Void doInBackground(Void... params) {
			xCo = mDragController.getX;
			yCo = mDragController.getY;
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			
			if (100 < yCo && yCo < 145) {
				imgMailBox.removeCallbacks(onEverySecond);
				Intent intent = new Intent(GuiBaiKiemTra.this, NhanKetQua.class);
				Bundle b = new Bundle();
				b.putString("dapanHS", dapanHS);
				intent.putExtras(b);
				startActivity(intent);
				finish();
			} else {
				imgMailBox.postDelayed(onEverySecond, 0);
			}
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return false;
		}
		return false;
	}
}
